package xuli.dao.impl;

import entity.Product_Info;
import entity.Product_Pic_Info;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.C3P0Util;
import xuli.dao.ProductInfoDao;

import java.sql.SQLException;
import java.util.List;
import java.util.Random;

public class ProductInfoDaoImpl implements ProductInfoDao {
    private QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());

    public Product_Info findProductById(Integer id) {
        String sql = "select * from product_info where product_id=?";
        Product_Info productInfo = null;
        try {
            productInfo = queryRunner.query(sql, new BeanHandler<>(Product_Info.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productInfo;
    }


    @Override
    public List<Product_Info> findRandom(Integer nums) {



        String sql = "select * from product_info order by rand() limit ?";
        List<Product_Info> productInfos = null;
        try {
            productInfos = queryRunner.query(sql, new BeanListHandler<>(Product_Info.class), nums);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return productInfos;
    }
}
